﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects.DataClasses;
using WA.Core.DomainModel;
using WA.Data;
using System.Data.Objects;
using WA.Core.DaoInterfaces;
using WA.Utility.EntityHelpers;
using WA.Core.Employees.Model;

[assembly: EdmSchema]

namespace WA.WinGUI
{
    public partial class Form1 : Form
    {
        WAObjectContext dc;
        int currentIndex;
        public Form1()
        {
            dc = new WA.Data.WAObjectContext();
            InitializeComponent();
            dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
        }

        void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Core.Employees.Model.FixedSalaryEmployee emp = new WA.Core.Employees.Model.FixedSalaryEmployee();
            emp.FirstName = tbFirstName.Text.Trim();
            emp.LastName = tbLastName.Text.Trim();
            emp.Salary = Decimal.Parse(tbSalary.Text.Trim());
            emp.SalaryCalculations = new List<SalaryCalculation>();          
           
            SalaryCalculation sal = new SalaryCalculation();
            sal.GrossSalary = Decimal.Parse(tbGrossSalary.Text.Trim());
            emp.SalaryCalculations.Add(sal);
           
           
            dc.AddObject("EmployeeSet", emp);
            dc.SaveChanges();
        }

        private void button2_Click(object sender, EventArgs e)
        {   
            //var lista = from emp in dc.Employees
                        //select emp;
            //IQueryable<FixedSalaryEmployee> fc= from fs in dc.Employee.OfType<FixedSalaryEmployee>().Include("SalaryCalculation")
            //                                     select fs;

            IQueryable<EntryInvoice> entryInvoice = from ei in dc.Invoices.OfType<EntryInvoice>()
                                                    select ei;
            var companies = from c in dc.Companies.Include("Invoice")
                            select c;

            MessageBox.Show(companies.ToList()[0].Invoices[0].InvoiceNumber);

           // dataGridView1.DataSource = companies;
            currentIndex = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //int id=Int32.Parse(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());           
            //var calc = from cal in dc.SalaryCalculation.Include("Employee")
            //           where cal.Employee.EmployeeID == id
            //           select new 
            //           {
            //               cal.Employee.EmployeeID,
            //               cal.GrossSalary,
            //               cal.SalaryCalculationID
            //           };                     
            //dataGridView2.DataSource = calc;
            IDaoFactory factory=new DaoFactory();
            IPartnerDao partnerDao = factory.GetPartnerDao();

            List<Partner> partners = partnerDao.GetAll();

            dataGridView2.DataSource = partners;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            Partner testPartner = PartnerHelper.GetRandomPartnerInstance();
            Company testCompany = CompanyHelper.GetRandomCompanyInstance();
            testPartner.Company = testCompany;

            dc.AddPartner(testPartner);
            dc.SaveChanges();
        }

        
    }
}
